/**
 * 
 */
package edu.hawaii.duedates.library;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import edu.hawaii.duedates.BorrowedItem;

/**
 * Tests the UhManoa class and its methods.
 * 
 * @author John Ly
 * @author John Zhou
 * 
 */
public class TestUhManoa {
  // the id and lastname arguments passed in at command-line
  private final String id = System.getProperty("uhm.id");
  private final String lastName = System.getProperty("uhm.lastname");
  private List<BorrowedItem> bookList = new ArrayList<BorrowedItem>();

  /**
   * Test getInfo with test accounts. Accounts provided by classmate, Phillip Lau, 
   * used for test purposes only.
   * 
   * @throws Exception If error logging in
   */
  @Test
  public void testGetInfo() throws Exception {
    // Test account 1 has 2 books checked out
    String testAccount1 = "09217690";
    // Test account 2 has no books checked out
    String testAccount2 = "101757897";
    String testLastName = "Test";
    UhManoa uhLib = new UhManoa();
    bookList = uhLib.getInfo(testAccount1, testLastName, bookList);
    assertFalse("Testing getInfo with books", bookList.isEmpty());
    assertEquals("Testing getInfo with books", bookList.size(), 2);
    assertTrue("Testing getInfo with books", bookList.get(0).getTitle().contains("Alexander")); 
    bookList.removeAll(bookList);
    assertTrue("Testing bookList empty", bookList.isEmpty());    
    
    bookList = uhLib.getInfo(testAccount2, testLastName, bookList);
    assertTrue("Testing getInfo with no books", bookList.isEmpty());
    
  }

  /**
   * Test login with a valid account. The test should pass if arguments correspond to a valid UH
   * account.
   * 
   * @throws Exception If error logging in
   */
  @Ignore("Emma does not pass arguments")
  @Test
  public void testLogin() throws Exception {
    UhManoa uhLib = new UhManoa();
    try {
      uhLib.login(this.id, this.lastName);
    }
    catch (Exception e) {
      fail("Valid account login failed");
    }
  }

  /**
   * Test login with a bad account arguments.
   * 
   * @throws Exception When error logging in.
   */
  @Test(expected = Exception.class)
  public void testIllegalLogin() throws Exception {
    UhManoa uhLib = new UhManoa();
    uhLib.login("", "");
    fail("Invalid account login passed");
  }
  
  /**
   * Test convertDateToCalendar.
   */
  @Test
  public void testConvertDateCalendar () {
    UhManoa uhLib = new UhManoa();
    Calendar cal = uhLib.convertDateToCalendar("22210-11-08");
    //Calendar month is zero-based.  So January is 0, February is 1, etc.
    assertEquals("Testing Month of Calendar", cal.get(Calendar.MONTH), 9);
    assertEquals("Testing Day of Calendar", cal.get(Calendar.DAY_OF_MONTH), 11);
    assertEquals("Testing Year of Calendar", cal.get(Calendar.YEAR), 2008);    
  }
}
